﻿using System;
using System.Linq;
using System.Windows.Forms;

namespace BigEaterCh
{
  public partial class FormJadlospisy : FormZywienie
  {
    private static FormJadlospisy _instance;

    public static FormJadlospisy Pokaz(Form parentForm)
    {
      if( _instance != null)
      {
        _instance.RefreshDb();
      }
      else
      {
        _instance = new FormJadlospisy
        {
          MdiParent = parentForm
        };
      }
      return _instance;
    }


    private FormJadlospisy()
    {
      InitializeComponent();
      RefreshDb();
    }

    private void FormJadlospisyFormClosing(object sender, FormClosingEventArgs e)
    {
      Hide();
      e.Cancel = true;
    }

    public void RefreshDb()
    {
      FormBigEaterCh.GetDataBase();
      jadlospisBindingSource.DataSource = FormBigEaterCh.Ze.Jadlospis;
    }

    private void ButtonOpenJadlospisClick(object sender, EventArgs e)
    {
      if( jadlospisBindingSource.Current != null )
      {
        var dp = (Jadlospis) jadlospisBindingSource.Current;

        var jadlospis = new FormJadlospis(dp)
        {
          MdiParent = MdiParent, Text = string.Format(@"Jadłospis {0} - {1}", dp.DataPoczatkowa.ToShortDateString(), dp.DataKoncowa.ToShortDateString())
        };
        jadlospis.Show();
        jadlospis.BringToFront();
      }
    }

    private void jadlospisBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
      FormBigEaterCh.SaveDataBase();
      FormBigEaterCh.Ze.Refresh(System.Data.Objects.RefreshMode.StoreWins, FormBigEaterCh.Ze.Jadlospis);
      
    }
  }
}
